<?php

namespace app\admin\controller\project;

use app\admin\controller\Base;
use think\Db;
use app\common\library\Tree;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Stories extends Base
{
    protected $beforeActionList = [
        'first',
        'second' => ['only' => 'storiesdelete,storieseditform'],
        'three' => ['only' => 'addtime,work_time_editform,work_time_delete,update_stories_status'],
    ];
    protected $project_user = [];
    protected $model_name = '';

    protected function first()
    {
        $post = $this->request->param();
        $info = Db::name('project_user')
            ->where('project_id', $post['project_id'])
            ->where('uid', session('uid'))
            ->find();
        if (!$info) {
            $this->error('你没有权限进入本模块');
        } else {
            $this->project_user = $info;
            if ($post['model_id'] > 4) {
                $post['model_id'] = 1;
            }
            $this->assign('project_id', $post['project_id']);
            $this->assign('model_id', $post['model_id']);
            $project_model = config('param.project_model');
            $this->assign('model_name', $project_model[$post['model_id']]);
            $this->model_name = $project_model[$post['model_id']];
            $model_status = config('param.model_' . $post['model_id'] . '_status');
            $this->assign('model_status', $model_status);
            //看有没有分类,没有分类增加一个
            $category = DB::name('model_category')
                ->where('project_id', $post['project_id'])
                ->where('model_id', $post['model_id'])
                ->cache(60)
                ->find();
            if (!$category) {
                $data['project_id'] = $post['project_id'];
                $data['model_id'] = $post['model_id'];
                $data['title'] = '项目';
                $data['lef'] = 1;
                $data['rig'] = 2;
                DB::name('model_category')->insert($data);
            }
        }
    }

    protected function second()
    {
        $info = $this->project_user;
        if ($info['user_type'] > 1) {
            $this->error('当前用户类型不能进行此操作');
        }

    }

    protected function three()
    {
        $info = $this->project_user;
        $user_type = config('param.project_user_type');
        if ($info['user_type'] == 3) {
            $this->error($user_type[3] . '不能进行此操作');
        }
    }


    public function wx_msg($info, $stories_id, $model_status = 0)
    {
        $post = $this->request->param();
        $data['project_id'] = $post['project_id'];
        $data['model_id'] = $post['model_id'];
        $data['info'] = $info;
        $data['stories_id'] = $stories_id;
        $data['c_time'] = time();
        $data['uid'] = session('uid');
        if ($model_status == 0) {
            $data['model_status'] = Db::name('stories')->where('id', $stories_id)->cache(60)->value('status');
        } else {
            $data['model_status'] = $model_status;
        }
        DB::name('wx_msg')->insert($data);
    }

    public function update_stories_status()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['stories_id'])) {
                $this->error('请求参数错误');
            }
            $need = Db::name('stories')->where(['id' => $post['stories_id']])->find();
            if ($need['status'] == 5) {
                $this->error('已经标记为完成的' . $this->model_name . '不能再次改变状态');
            }
            Db::startTrans();
            try {
                Db::name('stories')->where(['id' => $post['stories_id']])->update(['status' => $post['model_status']]);
                if ($post['model_status'] == 5) {
                    Db::name('stories')->where(['id' => $post['stories_id']])->update(['f_time' => time()]);
                }
                $this->wx_msg('项目成员改变了' . $this->model_name . '状态', $post['stories_id'], $post['model_status']);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');
        }
    }

    public function update_stories_model()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['stories_id'])) {
                $this->error('请求参数错误');
            }
            $need = Db::name('stories')->where(['id' => $post['stories_id']])->find();
            if ($need['status'] == 4) {
                $this->error('已经标记为完成的' . $this->model_name . '不能改变模型');
            }
            Db::startTrans();
            try {
                Db::name('stories')->where(['id' => $post['stories_id']])->update(['model_id' => $post['new_model_id']]);
                $new_model_name = config('param.project_model');
                $this->wx_msg('项目成员操作将模型由' . $this->model_name . '转换为' . $new_model_name[$post['new_model_id']], $post['stories_id'], $need['status']);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');
        }
    }

    public function work_time_delete()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['id'])) {
                $this->error('请求参数错误');
            }
            $info = DB::name('work_time')->where('id', $post['id'])->find();
            $status = DB::name('stories')->where('id', $info['stories_id'])->value('status');
            if ($status == 5) {
                $this->error($this->model_name . '已经操作结束不能进行工时操作');
            }
            Db::startTrans();
            try {
                Db::name('work_time')->where(['id' => $post['id']])->delete();
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }
    }

    public function work_time_editform()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            //单个字段修改
            $info = DB::name('work_time')->where('id', $post['id'])->find();
            $status = DB::name('stories')->where('id', $info['stories_id'])->value('status');
            if ($status == 5) {
                $this->error($this->model_name . '已经操作结束不能进行工时操作');
            }
            Db::startTrans();
            try {
                if ($this->request->param('w_time/d') < 1) {
                    $this->error('分钟数错误，请检查');
                }
                Db::name('work_time')->where(['id' => $post['id']])->update($post);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }
    }

    public function work_time_list()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        if (empty($post['title']) || $post['title'] == '') {
        } else {
            $map[] = ['title', 'like', '%' . $post['title'] . '%'];
        }
        if (empty($post['status']) || $post['status'] == '') {

        } else {
            $map[] = ['status', '=', $post['status']];
        }
        $map[] = ['work_time.stories_id', '=', $post['id']];
        $data['count'] = Db::view('work_time', 'id,uid,c_time,w_time')
            ->view('admin', 'nickname', 'work_time.uid=admin.id')
            ->where($map)
            ->count();
        $data['data'] = Db::view('work_time', 'id,uid,c_time,w_time')
            ->view('admin', 'nickname', 'work_time.uid=admin.id')
            ->where($map)
            // ->fetchSql(true)
            ->order('work_time.id desc')->page(input("param.page"), input('param.limit'))->select();

        foreach ($data['data'] as &$info) {
            $info['c_time'] == 0 ? $info['c_time'] = '--' : $info['c_time'] = date('Y-m-d   H:i:s', $info['c_time']);
        }

        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function addtime()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if ($this->request->param('w_time', 0, 'intval') < 0) {
                $this->error('工作时间数应当大于0，请检查再提交');
            }
            $status = DB::name('stories')->where('id', $post['stories_id'])->value('status');
            if ($status == 5) {
                $this->error($this->model_name . '已经操作完成不能进行工时操作');
            }
            Db::startTrans();
            try {
                $post['c_time'] = time();
                $post['uid'] = session('uid');
                Db::name('work_time')->strict(false)->insert($post);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }
    }

    public function addcomment()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();

            Db::startTrans();
            try {
                $post['c_time'] = time();
                $post['uid'] = session('uid');
                $post['info'] = $this->request->param('info', '', null);
                Db::name('comment')->strict(false)->insert($post);
                $this->wx_msg('添加新的' . $this->model_name . '讨论(' . $post['info'] . ')', $post['stories_id']);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }
    }

    public function comment_list()
    {
        $data = array();
        $post = $this->request->param();
        unset($post['page']);
        unset($post['limit']);
        unset($post['keyword']);
        $data['count'] = DB::view('comment', 'id,uid,c_time,info')
            ->view('admin', 'nickname', 'comment.uid=admin.id')
            ->where('stories_id', $post['stories_id'])
            ->count();
        $data['data'] = DB::view('comment', 'id,uid,c_time,info')
            ->view('admin', 'nickname', 'comment.uid=admin.id')
            ->where('stories_id', $post['stories_id'])
            ->order('id desc')->page(input("param.page"), input('param.limit'))->select();

        foreach ($data['data'] as &$item) {
            $item['c_time'] = date('Y-m-d  H:i:s', $item['c_time']);
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }


    public function getcommentcount()
    {
        $data['data'] = Db::name('comment')
            ->where('stories_id', input('param.stories_id'))
            ->count();
        if ($data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    /*
     * 新内容
     * */
    public function index()
    {
        return view();
    }

    public function storieslists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        $map[] = ['stories.delete_time', '=', 0];
        if (empty($post['title']) || $post['title'] == '') {
        } else {
            $map[] = ['stories.title', 'like', '%' . $post['title'] . '%'];
        }
        if (empty($post['status']) || $post['status'] == '') {

        } else {
            $map[] = ['stories.status', '=', $post['status']];
        }
        if (empty($post['category']) || $post['category'] == '') {

        } else {
            $info = DB::name('model_category')
                ->where('id', $post['category'])
                ->find();
            $category_list = DB::name('model_category')
                ->where('lef', '>=', $info['lef'])
                ->where('rig', '<=', $info['rig'])
                ->field('id')
                ->select();

            $category_list = array_column($category_list, 'id');
            $map[] = ['stories.category', 'in', implode(',', $category_list)];
        }
        $map[] = ['stories.project_id', '=', $post['project_id']];
        $map[] = ['stories.model_id', '=', $post['model_id']];
        $data['count'] = Db::view('stories', 'id,project_id,title,touid,s_time,e_time,w_time,uid,p_time,level_id,status,delete_time,category,model_id')
            ->view(['admin' => 'adminuid'], ['nickname' => 'uidnickname'], 'stories.uid=adminuid.id', 'LEFT')
            ->view(['admin' => 'admintouid'], ['nickname' => 'touidnickname'], 'stories.uid=admintouid.id', 'LEFT')
            ->where($map)
            ->count();
        $data['data'] = Db::view('stories', 'id,project_id,title,touid,s_time,e_time,w_time,uid,p_time,level_id,status,delete_time,category,model_id')
            ->view(['admin' => 'adminuid'], ['nickname' => 'uidnickname'], 'stories.uid=adminuid.id', 'LEFT')
            ->view(['admin' => 'admintouid'], ['nickname' => 'touidnickname'], 'stories.touid=admintouid.id', 'LEFT')
            ->where($map)
            //->fetchSql(true)
            ->order(['status', 'id' => 'desc',])->page(input("param.page"), input('param.limit'))->select();
        //echo $data['data'];
        $color = config('param.set_color');
        $list = DB::name('model_category')
            ->where('project_id', $post['project_id'])
            ->where('model_id', $post['model_id'])
            ->field('id,pid,title')
            ->select();
        $stories_status = config('param.model_' . $post['model_id'] . '_status');
        $level_status = config('param.level_status');
        foreach ($data['data'] as &$info) {
            //如果是完成任务加一个标记或颜色
            $new_color=0;
            foreach ($list as $key=>$info2) {
                if ($info2['id'] == $info['category']) {
                    $new_color = substr($key, -1);
                }
            }
            if($info['status']==5){
                $info['title'] = '<div style="color:' . $color[(int)$new_color] . ';text-decoration:line-through;">' . $info['title'] . '</div>';

            }else{
                $info['title'] = '<div style="color:' . $color[(int)$new_color] . '">' . $info['title'] . '</div>';
            }
            $info['status'] = $stories_status[$info['status']];
            $info['level_id'] = $level_status[$info['level_id']];
            $info['s_time'] == 0 ? $info['s_time'] = '--' : $info['s_time'] = date('m-d', $info['s_time']);
            $info['e_time'] == 0 ? $info['e_time'] = '--' : $info['e_time'] = date('m-d', $info['e_time']);
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function getstoriesinfo()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        $info = Db::view('stories', 'id,project_id,title,touid,s_time,e_time,w_time,uid,p_time,level_id,status,delete_time,info_id,model_id,category,f_time')
            ->view(['admin' => 'adminuid'], ['nickname' => 'uidnickname'], 'stories.uid=adminuid.id', 'LEFT')
            ->view(['admin' => 'admintouid'], ['nickname' => 'touidnickname'], 'stories.touid=admintouid.id', 'LEFT')
            ->view('model_category', ['title' => 'category_title'], 'stories.category=model_category.id')
            ->where('stories.id', $post['stories_id'])
            //->cache(60)
            ->find();
        $stories_status = config('param.model_' . $post['model_id'] . '_status');
        $level_status = config('param.level_status');
        $info['status'] = $stories_status[$info['status']];
        $info['level_id'] = $level_status[$info['level_id']];
        $info['s_time'] == 0 ? $info['s_time'] = '' : $info['s_time'] = date('Y-m-d', $info['s_time']);
        $info['e_time'] == 0 ? $info['e_time'] = '' : $info['e_time'] = date('Y-m-d', $info['e_time']);
        $info['f_time'] == 0 ? $info['f_time'] = '' : $info['f_time'] = date('Y-m-d H:i:s', $info['f_time']);
        $info['info'] = Db::name('infoview')
            ->where('id', $info['info_id'])
            ->value('info');
        $data['data'] = $info;
        $data['msg'] = '获取成功';
        $data['code'] = 0;
        return json($data);
    }


    public function storieswork()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            //单个字段修改
            if (count($post) == 3) {

                Db::startTrans();
                try {
                    Db::name('project')->where(['id' => $post['id']])->update($post);
                    Db::commit();
                    $status = 1;
                } catch (\Exception $e) {
                    Db::rollback();
                    $status = 0;
                }
            } else {
                $validate = new \app\admin\validate\Stories();
                if (!$validate->check($post)) {
                    $this->error($validate->getError());
                }
                Db::startTrans();
                try {
                    Db::name('infoview')->where(['id' => $post['info_id']])->update(['info' => $this->request->param('info', '', null)]);
                    unset($post['info']);
                    $post['s_time'] = strtotime($post['s_time']);
                    $post['e_time'] = strtotime($post['e_time']);
                    Db::name('stories')->where(['id' => $post['id']])->update($post);
                    Db::commit();
                    $status = 1;
                } catch (\Exception $e) {
                    Db::rollback();
                    $status = 0;
                }
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');
        } else {
            $this->assign('model', config('param.project_model'));
            $this->assign('stories_status', config('param.stories_status'));
            $this->assign('id', input('param.id'));
            return view('storieswork');
        }
    }

    public function editform()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {
            //单个字段修改
            if (count($post) == 4) {
                foreach ($post as $key => $info) {
                    if ($key == 'w_time') {
                        $this->error('实际时间不能修改');
                    }
                    if ($key == 'p_time') {
                        $this->error('预期时间不能修改');
                    }
                }
                Db::startTrans();
                try {
                    Db::name('stories')->where(['id' => $post['id']])->update($post);
                    $this->wx_msg($this->model_name . '进行了编辑', $post['id']);
                    Db::commit();
                    $status = 1;
                } catch (\Exception $e) {
                    Db::rollback();
                    $status = 0;
                }
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {
            $this->assign('stories_status', config('param.stories_status'));
            $this->assign('stories_source', config('param.stories_source'));
            $this->assign('level_status', config('param.level_status'));
            $info = Db::name('stories')
                ->where('id', input('param.id'))
                ->find();
            $userlist = Db::view('project_user', 'project_id,uid')
                ->view('admin', 'id,nickname', 'project_user.uid=admin.id')
                ->where('project_user.project_id', $info['project_id'])
                ->select();
            $this->assign('userlist', $userlist);
            $info['s_time'] == 0 ? $info['s_time'] = '' : $info['s_time'] = date('Y-m-d', $info['s_time']);
            $info['e_time'] == 0 ? $info['e_time'] = '' : $info['e_time'] = date('Y-m-d', $info['e_time']);
            $info['info'] = Db::name('infoview')
                ->where('id', $info['info_id'])
                ->value('info');
            $list = DB::name('model_category')
                ->where('project_id', $post['project_id'])
                ->where('model_id', $post['model_id'])
                ->field('id,pid,title')
                ->select();
            $category = Tree::unlimitForLevel($list);
            $this->assign('category', $category);
            $this->assign('info', $info);
            return view('storiesadd');
        }

    }

    public function delete()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['id'])) {
                $this->error('请求参数错误');
            }
            $info = Db::name('stories')->where(['id' => $post['id']])->find();
            if ($info['status'] == 5) {
                $this->error('标记完成的' . $this->model_name . '不能删除');
            }
            Db::startTrans();
            try {
                $info = Db::name('stories')->where(['id' => $post['id']])->useSoftDelete('delete_time', time())->delete();
                $this->wx_msg('删除' . $this->model_name . '操作', $post['id']);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }
    }

    public function storiesadd()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {
            $validate = new \app\admin\validate\Stories();
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            if ($post['id']) {
                Db::startTrans();
                try {
                    Db::name('infoview')->where(['id' => $post['info_id']])->update(['info' => $this->request->param('info', '', null)]);
                    unset($post['info']);
                    if ($post['s_time'] == $post['e_time']) {
                        //时间短直接加一天
                        $post['e_time'] = strtotime($post['s_time']) + 172799;
                    } else {
                        $post['e_time'] = strtotime($post['e_time'] . ' 23:59:59');
                    }
                    $post['s_time'] = strtotime($post['s_time']);
                    Db::name('stories')->where(['id' => $post['id']])->update($post);
                    $this->wx_msg('编辑' . $this->model_name . '操作', $post['id']);
                    Db::commit();
                    $status = 1;
                } catch (\Exception $e) {
                    Db::rollback();
                    $status = 0;
                }
            } else {
                $info = $this->project_user;
                if ($info['user_type'] > 1 && $post['status'] <> 1) {
                    $this->error('当前用户类型不支持使用当前选择需求状态');
                }
                if (substr($post['s_time'], 0, 7) != substr($post['e_time'], 0, 7)) {
                    $this->error('添加新的' . $this->model_name . '开始日期和结束日期要在同一个月份');
                }
                Db::startTrans();
                try {
                    $post['info_id'] = Db::name('infoview')->insertGetId(['info' => $this->request->param('info', '', null)]);
                    unset($post['info']);
                    if ($post['s_time'] == $post['e_time']) {
                        //时间短直接加一天
                        $post['e_time'] = strtotime($post['s_time']) + 172799;
                    } else {
                        $post['e_time'] = strtotime($post['e_time'] . ' 23:59:59');
                    }
                    $post['s_time'] = strtotime($post['s_time']);
                    $post['uid'] = session('uid');
                    unset($post['id']);
                    $post['id'] = Db::name('stories')->strict(false)->insertGetId($post);
                    $this->wx_msg('新增' . $this->model_name . '操作', $post['id']);
                    Db::commit();
                    $status = 1;
                } catch (\Exception $e) {
                    Db::rollback();
                    $status = 0;
                }
            }

            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');
        } else {
            $this->assign('level_status', config('param.level_status'));
            $this->assign('info', ['project_id' => input('param.project_id')]);
            $userlist = Db::view('project_user', 'project_id,uid')
                ->view('admin', 'id,nickname', 'project_user.uid=admin.id')
                ->where('project_user.project_id', $post['project_id'])
                ->select();

            $list = DB::name('model_category')
                ->where('project_id', $post['project_id'])
                ->where('model_id', $post['model_id'])
                ->field('id,pid,title')
                ->select();
            $category = Tree::unlimitForLevel($list);
            $this->assign('category', $category);
            $this->assign('userlist', $userlist);
            return view();
        }
    }

    public function category()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (count($post) == 2) {
                $this->success('操作完成');
            }
            $newdata = array();
            switch ($post['operate']) {
                case 'add':
                    $newdata['project_id'] = $post['project_id'];
                    $newdata['model_id'] = $post['model_id'];
                    $newdata['pid'] = $post['pid'];
                    $newdata['title'] = '未命名';
                    $data['data'] = DB::name('model_category')
                        ->insertGetId($newdata);
                    break;
                default:


            }
            if ($data['data']) {
                $data['msg'] = '获取成功';
            } else {
                $data['msg'] = '获取数据为空';
            }
            $data['code'] = 0;
            return json($data);

        } else {
            return view();
        }
    }

    public function addcategory()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            //单个字段修改
            Db::startTrans();
            try {
                $pinfo = DB::name('model_category')
                    ->where('project_id', $post['project_id'])
                    ->where('model_id', $post['model_id'])
                    ->where('id', $post['pid'])
                    ->find();
                DB::name('model_category')
                    ->where('project_id', $post['project_id'])
                    ->where('model_id', $post['model_id'])
                    ->where('rig', '>=', $pinfo['rig'])
                    ->setInc('rig', 2);
                $post['lef'] = $pinfo['rig'];
                $post['rig'] = $pinfo['rig'] + 1;
                Db::name('model_category')->insert($post);

                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');
        }

    }

    public function getcategory()
    {
        $post = $this->request->param();
        $list = DB::name('model_category')
            ->where('project_id', $post['project_id'])
            ->where('model_id', $post['model_id'])
            ->field('id,pid,title')
            ->select();

        $data['data'] = Tree::unlimitForLevel($list);
        if ($data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function treecategory()
    {
        $post = $this->request->param();
        $list = DB::name('model_category')
            ->where('project_id', $post['project_id'])
            ->where('model_id', $post['model_id'])
            ->field('id,pid,title')
            ->select();
        $color = config('param.set_color');
        foreach ($list as $key => &$item) {
            $item['spread'] = true;
            $new_color = substr($key, -1);
            $item['title'] = '<div style="color:' . $color[(int)$new_color] . '">' . $item['title'] . '</div>';
        }

        $data['data'] = Tree::unlimitForLayer($list, 0, 'children');
        if ($data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function delcategory()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['id'])) {
                $this->error('请求参数错误');
            }
            if ($post['pid'] == 0) {
                $this->error('根分类不能删除');
            }
            Db::startTrans();
            try {
                $info = Db::name('model_category')->where('id', $post['id'])->find();
                DB::name('model_category')
                    ->where('project_id', $post['project_id'])
                    ->where('model_id', $post['model_id'])
                    ->where('rig', '>=', $info['rig'])
                    ->setDec('rig', 2);
                DB::name('model_category')
                    ->where('project_id', $post['project_id'])
                    ->where('model_id', $post['model_id'])
                    ->where('lef', '>', $info['lef'])
                    ->setDec('lef', 2);
                Db::name('stories')->where('category', $post['id'])->update(['category' => $post['pid']]);
                Db::name('model_category')->where('id', $post['id'])->delete();
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');
        }
    }

    public function editcategory()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            //单个字段修改
            Db::startTrans();
            try {
                Db::name('model_category')
                    ->where('project_id', $post['project_id'])
                    ->where('model_id', $post['model_id'])
                    ->where(['id' => $post['id']])
                    ->update($post);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }

    }

}